//
//  ALPS_SpecialIntroViewController.m
//  Alps
//
//  Created by dllo on 16/6/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_SpecialIntroViewController.h"
#import "WSSNetTool.h"
#import "ALPS_RecommendTableViewCell.h"
#import "ALPS_MainModel.h"
#import "ALPS_ClassifyClickClickAlbumController.h"
#import "ALPS_PlayerMainViewController.h"
#import "ALPS_SpecialDetailModel.h"

@interface ALPS_SpecialIntroViewController ()<UITableViewDelegate, UITableViewDataSource>;
/** 主界面 */
@property (nonatomic, strong) UITableView *tableView;
/** 存放mainModel数据源数组 */
@property (nonatomic, strong) NSMutableArray *dataSource;
/** 存放specialDetailModel的数组 */
@property (nonatomic, strong) NSMutableArray *specialDataSource;
/** 种类 */
@property (nonatomic, assign) NSInteger contentType;
/** 头视图 */
@property (nonatomic, strong) UIView *headerView;
/** 头视图标题 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 头视图简介 */
@property (nonatomic, strong) UILabel *introLabel;
/** 头视图的简介图片 */
@property (nonatomic, strong) UIImageView *introImageView;
/** 播放页数组 */
@property (nonatomic, strong) NSMutableArray *trackIdArr;
@end

@implementation ALPS_SpecialIntroViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setMainTableView];
    [self getDataBySpecialId:self.specialId];
    [self setLeftBackButton];
}

- (void)getDataBySpecialId:(NSInteger)specialId
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?device=android&id=%ld", specialId] body:nil headers:nil response:WSSJSON success:^(id result) {
        NSArray *arr = [result objectForKey:@"list"];
        self.contentType = [[[result objectForKey:@"info"] objectForKey:@"contentType"] integerValue];
        self.dataSource = [NSMutableArray array];
        self.trackIdArr = [NSMutableArray array];
        self.specialDataSource = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            ALPS_MainModel *model = [[ALPS_MainModel alloc] initWithDic:dic];
            [self.dataSource addObject:model];
            ALPS_SpecialDetailModel *spModel = [[ALPS_SpecialDetailModel alloc] initWithDic:dic];
            [self.trackIdArr addObject:spModel];
        }
        //为头视图赋值
        self.titleLabel.text = [[result objectForKey:@"info"] objectForKey:@"title"];
        self.introLabel.text = [[result objectForKey:@"info"] objectForKey:@"intro"];
        CGSize size = [self.introLabel sizeThatFits:CGSizeMake(self.introLabel.frame.size.width, MAXFLOAT)];
        self.introLabel.frame = CGRectMake(10, self.introImageView.frame.size.height + self.introImageView.frame.origin.y + 10, ScreeWidth - 20, size.height);
        self.headerView.frame = CGRectMake(0, 0, ScreeWidth, self.introLabel.frame.size.height + self.introLabel.frame.origin.y + 20);
        self.tableView.tableHeaderView = _headerView;
        //分割线
        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.headerView.frame.size.height - 10, ScreeWidth, 10)];
        lineLabel.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
        [self.headerView addSubview:lineLabel];
        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (void)setMainTableView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight - 64) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    //设置头视图
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, 300)];
    
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 32.5, ScreeWidth / 7.5, ScreeWidth / 7.5)];
    leftImageView.image = [UIImage imageNamed:@"findsubject_cover"];
    [self.headerView addSubview:leftImageView];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(leftImageView.frame.origin.x + leftImageView.frame.size.width + 10, 20, ScreeWidth - leftImageView.frame.origin.x - leftImageView.frame.size.width - 20, leftImageView.frame.size.height * 1.5)];
    self.titleLabel.numberOfLines = 2;
    self.titleLabel.font = [UIFont systemFontOfSize:22];
    [self.headerView addSubview:_titleLabel];
    
    self.introImageView = [[UIImageView alloc] initWithFrame:CGRectMake(90, self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + 10, ScreeWidth - 180, 17)];
    self.introImageView.image = [UIImage imageNamed:@"findsubject_segline"];
    [self.headerView addSubview:_introImageView];
    
    self.introLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.introImageView.frame.size.height + self.introImageView.frame.origin.y + 10, ScreeWidth - 20, 50)];
    self.introLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
    self.introLabel.font = [UIFont systemFontOfSize:15];
    self.introLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.introLabel.numberOfLines = 0;
    [self.headerView addSubview:_introLabel];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreeHeight / 6.67;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

#pragma mark -- sectionHeader样式
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, 50)];
    
    UIImageView *leftArrow = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 30, 30)];
    leftArrow.image = [UIImage imageNamed:@"toolbar_play_n"];
    [headerView addSubview:leftArrow];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(28, 10, ScreeWidth, 30)];
    label.text = @"听单推荐";
    label.font = [UIFont systemFontOfSize:17];
    
    [headerView addSubview:label];
    
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_RecommendTableViewCell *cell = [[ALPS_RecommendTableViewCell alloc] init];
    cell.selectionStyle = 0;
    cell.contentType = self.contentType;
    cell.recommendModel = [self.dataSource objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark -- 取消tableViewSection的浮动效果.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y <= sectionHeaderHeight&&scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //进入专辑页
    if (self.contentType == 1) {
        ALPS_ClassifyClickClickAlbumController *CCCAC = [[ALPS_ClassifyClickClickAlbumController alloc] init];
        CCCAC.albumId = [[self.dataSource objectAtIndex:indexPath.row] id];
        [self.navigationController pushViewController:CCCAC animated:YES];
        //self.contentType == 2 进入播放页
    } else {
        ALPS_PlayerMainViewController *PMVC = [[ALPS_PlayerMainViewController alloc] init];
        PMVC.trackId = [[self.dataSource objectAtIndex:indexPath.row] id];
        PMVC.listArr = self.trackIdArr;
        [self.navigationController pushViewController:PMVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
